package com.abou.model.admin;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Transient;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
import org.hibernate.validator.constraints.Length;
import org.hibernate.validator.constraints.NotEmpty;

/**
 * The class storing information on a user account.
 * 
 * @author khalid
 */
@Entity
public class Account extends com.abou.model.common.Entity {
  
  /**
   *
   */
  private static final long serialVersionUID = 1L;
  
  /**
   * The login field name
   */
  public static final String LOGIN_ATT = "login";
  
  /**
   * The GUI password field name
   */
  public static final String PWD_ATT = "password";
  
  /**
   * The GUI confirmation password field name
   */
  public static final String PWD_CONFIRMATION_ATT = "confirmPassword";
  
  /**
   * The regexp the GUI password must match
   */
  public static final String PWD_REGEXP =
      "^.*(?=.*[a-z])(?=.*[A-Z])(?=.*[\\d])(?=.*[\\W\\p{Punct}]).*$";
  /**
   * Password must have a length min = 6 to max = 15 and must contains at least
   * 1 lower case, 1 upper case, 1 digit and 1 special char
   */
  @Column(nullable = false)
  @NotNull
  @NotEmpty
  @Length(min = 6, max = 15)
  @Pattern(regexp = PWD_REGEXP)
  private String password;
  
  @Transient
  private String confirmPassword;
  
  @NotNull
  @NotEmpty
  private String login;
  
  @Id
  @Column(nullable = false)
  @NotNull
  @NotEmpty
  private String email;
  
  /**
   * @param email the user email
   */
  public void setEmail(String email) {

    this.email = email;
  }
  
  /**
   * @return The email user
   */
  public String getEmail() {

    return email;
  }
  
  /**
   * The default constructor
   */
  public Account() {

    super();
  }
  
  /**
   * @param login The user login to access the application
   * @param password See {@link #getPassword()}.
   */
  public Account(String login, String password) {

    this.password = password;
    this.login = login;
  }
  
  /**
   * @return The user password to access the application
   */
  public String getPassword() {

    return password;
  }
  
  /**
   * @return The login
   */
  public String getLogin() {

    return login;
  }
  
  /**
   * @return The confirmation password for GUI account creation
   */
  public String getConfirmPassword() {

    return confirmPassword;
  }
  
  /**
   * @param confirmPassword The confirmation password for GUI account creation
   */
  public void setConfirmPassword(String confirmPassword) {

    this.confirmPassword = confirmPassword;
  }
  
  /**
   * Sets the new value for the password field.
   * 
   * @param password The new value for the password field
   */
  public void setPassword(String password) {

    this.password = password;
  }
  
  /**
   * Sets the new value for the login field.
   * 
   * @param login The new value for the login field
   */
  public void setLogin(String login) {

    this.login = login;
  }
  
  /**
   * Checks if the password and the confirmation password are equals.
   * 
   * @return Wether the password and the confirmation password are equals.
   */
  public boolean checkConfirmationPassword() {

    return confirmPassword.equals(password);
  }
}